package com.ecnu.sei.app.pcm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import com.ecnu.sei.app.course.AbstractCourse;
import com.ecnu.sei.app.course.CompulsoryCourse;
import com.ecnu.sei.app.course.CourseInfo;
import com.ecnu.sei.app.course.SpecialtyElective;
import com.ecnu.sei.app.resources.Classroom;
import com.ecnu.sei.app.teacher.AbstractTeacher;
import com.ecnu.sei.app.util.Pair;
import com.ecnu.sei.app.util.Utils.DayHour;

public class GenerateCourseData extends AbstractJSONConversion {
  private transient GenerateData genData = new GenerateData();

  /*  private String courseId = null;
  private String className = null;
  private String courseName = null;
  private CollegeYear collegeYear = CollegeYear.FRESHMAN;
  private String teacherId = null;
  private AbstractTeacher teacher;
  private int studentNum = 0;

   */ 
  private Map<Pair<Integer, DayHour>, Set<Classroom>> resourceMap = new HashMap<Pair<Integer, DayHour>, Set<Classroom>>();
  private List<AbstractTeacher> teacherList;
  private int rangeStart = 0, rangeEnd = 0;
  private List<Pair<Integer, DayHour>> courseTimeList;
//  private List<Classroom> classroomList;
  /*	public void setCourseIdLength(int length) {
		genData.setCourseIdLength(length);
		this.courseId = genData.generateCourseId();
	}

	public void setTeacherIdLength(int length) {
		genData.setTeacherIdLength(length);
		this.teacherId = genData.generateTeacherId();
	}
   */
  /*  public void setCourseCount(int count) {
    genData.setCourseCount(count);
    this.courseName = genData.generateCourseName();
  }
   */
  public void setTeacherDelegate(int teacherNum1, int teacherNum2,
      int teacherNum3, int teacherNum4, int teacherNum5, int teacherNum6) {
    this.teacherList = genData.generateTeacher(teacherNum1, teacherNum2,
        teacherNum3, teacherNum4, teacherNum5, teacherNum6);
  }

  public void setStudentNumRange(int rangeStart, int rangeEnd) {
    //this.studentNum = genData.generateStudentNumber(rangeStart, rangeEnd);
    this.rangeStart = rangeStart;
    this.rangeEnd = rangeEnd;
  }

  public GenerateCourseData() {
    /*    this.courseId = genData.generateCourseId();
    this.className = genData.generateClassName();
    this.courseName = genData.generateCourseName();
    this.collegeYear = genData.deriveCollegeYear(this.className);
    this.teacherId = genData.generateTeacherId();
     */  

 //   this.classroomList = genData.generateClassroomList();
    this.courseTimeList = genData.generateCourseTimeList();
  }

  /*  public GenerateCompulsoryData(int teacherNum1,
      int teacherNum2,
      int teacherNum3,
      int teacherNum4,
      int teacherNum5,
      int teacherNum6,
      int studentNumRangeStart,
      int studentNumRangeEnd) {
    this.courseId = genData.generateCourseId();
    this.className = genData.generateClassName();
    this.courseName = genData.generateCourseName();
    this.collegeYear = genData.deriveCollegeYear(this.className);
    this.teacherId = genData.generateTeacherId();
    this.teacherList = genData.generateTeacher(teacherNum1, teacherNum2,
        teacherNum3, teacherNum4, teacherNum5, teacherNum6);
    this.courseTimeList = genData.generateCourseTimeList();
    this.studentNum = genData.generateStudentNumber(studentNumRangeStart,
        studentNumRangeStart);
    this.classroomList = genData.generateClassroomList();
  }
   */
  /*	public GenerateCompulsoryData(
			int courseIdLength,
			int courseCount,
			int teacherNameLength,
			int teacherNum1,
			int teacherNum2,
			int teacherNum3,
			int teacherNum4,
			int teacherNum5,
			int teacherNum6,
			int studentNumRangeStart,
			int studentNumRangeEnd) {
		GenerateData newGenData = new GenerateData();

		newGenData.setCourseIdLength(courseIdLength);
		newGenData.setCourseCount(courseCount);
		newGenData.setTeacherIdLength(teacherNameLength);

		this.courseId = newGenData.generateCourseId();
		this.className = newGenData.generateClassName();
		this.courseName = newGenData.generateCourseName();
		this.collegeYear = newGenData.deriveCollegeYear();
		this.teacherId = newGenData.generateTeacherId();
		this.teacherList = newGenData.generateTeacher(teacherNum1, teacherNum2,
				teacherNum3, teacherNum4, teacherNum5, teacherNum6);
		this.courseTimeList = newGenData.generateCourseTimeList();
		this.studentNum = newGenData.generateStudentNumber(
				studentNumRangeStart, studentNumRangeStart);
		this.classroomList = newGenData.generateClassroomList();
	}
   */
  /*  public GenerateData getGenData() {
    return genData;
  }
   */
  public AbstractCourse genCompulsoryCourse() {
    return new CompulsoryCourse(genCourseInfo());
  }

  public AbstractCourse genElectiveCourse() {
    return new SpecialtyElective(genCourseInfo());
  }

  private CourseInfo genCourseInfo() {
    GenerateData genData = new GenerateData();
    CourseInfo courseInfo = new CourseInfo();
    Random random = new Random(System.currentTimeMillis());
    courseInfo.setCourseId(genData.generateCourseId())
    .setCourseName(genData.generateCourseName())
    .setStudentNum(genData.generateStudentNumber(rangeStart, rangeEnd))
    .setTeacher(teacherList.get(Math.abs(random.nextInt()) % teacherList.size()));

    /*    for (Classroom classroom : classroomList) {
      courseInfo.addClassroom(classroom);
    }

    for (Pair<Integer, DayHour> courseTime : courseTimeList) {
      courseInfo.addCourseTime(courseTime);
    }*/
    genResource(courseInfo);
    
    return courseInfo; 
  }

  private void genResource(CourseInfo courseInfo) {
    Random random = new Random(System.currentTimeMillis());
    Pair<Integer, DayHour> time = courseTimeList.get(Math.abs(random.nextInt() % courseTimeList.size()));
    Classroom classroom = new Classroom(String.valueOf(random.nextInt()));
    if (null == resourceMap.get(time)) {
      resourceMap.put(time, new HashSet<Classroom>());
    }
    while (resourceMap.get(time).contains(classroom)) {
      classroom = new Classroom(String.valueOf(random.nextInt()));
    }

    resourceMap.get(time).add(classroom);

    courseInfo.addClassroom(classroom);
    courseInfo.addCourseTime(time);

  }
  public List<AbstractCourse> genCompulsoryCourseList(int courseNum) {
    List<AbstractCourse> courseList = new ArrayList<AbstractCourse>();
    for (int i = 0; i < courseNum; i++) {
      courseList.add(genCompulsoryCourse());
    }
    return courseList;
  }

  public List<AbstractCourse> genElectiveCourseList(int courseNum) {
    List<AbstractCourse> courseList = new ArrayList<AbstractCourse>();
    for (int i = 0; i < courseNum; i++) {
      courseList.add(genElectiveCourse());
    }
    return courseList;

  }

  public List<AbstractTeacher> getTeacherList() {
    return teacherList;
  }

  /*  public AbstractTeacher getTeacher() {
    return teacher;
  }
   */

}
